package com.reins.bookstore.controller;

import com.reins.bookstore.object.*;
import com.reins.bookstore.service.BookService;
import com.reins.bookstore.service.OrderService;
import com.reins.bookstore.service.UserService;
import com.reins.bookstore.utils.pageutils.Page;
import com.reins.bookstore.utils.pageutils.PageUtil;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@RestController
public class StatisticsController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private BookService bookService;

    @Autowired
    private UserService userService;


    @RequestMapping("/getMostExpensiveBooks")
    public List<BookRate> getMostExpensiveBooks(@RequestParam("front_idx")Integer front_idx, @RequestParam("end_idx")Integer end_idx,@RequestParam("top_k")Integer top_k){
        return bookService.getMostExpensiveBooks(front_idx,end_idx,top_k);
    }

    @RequestMapping("/getBookRate")
    public List<BookRate> getUserRate(@RequestBody Filter params){
        return bookService.getBookRate(params);
    }

    @RequestMapping("/getMostHotBooks")
    public List<BookRate> getMostHotBooks(@RequestParam("top") Integer top_k){
        return bookService.getMostHotBooks(top_k);

    }

    @RequestMapping("/getBookRateById")
    public List<BookRate> getUserRate(@RequestBody Filter params,@RequestParam("userId")Integer userId){
        return bookService.getBookRate(params);
    }

    @RequestMapping("/getAllBookRate")
    public Page getAllUserRate(@RequestBody Filter filter){
        if (filter.getPages() == null)
            filter.setPages(1);     // 默认是第一页
        if (filter.getUserId()== null)
            filter.setUserId(0);    // 默认是全用户
        List<BookRate> get = bookService.getBookRate(filter);
        JSONArray data = JSONArray.fromObject(get);
        Page result = PageUtil.makePage(filter.getPages(),data);
        return result;
    }
    @RequestMapping("/getAllBookRateById")
    public List<BookRate> getAllUserRate(@RequestParam("userId")Integer userId)
    {
        return bookService.getBookRate(null);
    }
    @RequestMapping("/testTime")
    public String testTime(@RequestBody Filter params){
        Filter get = params;
        return "hello";
    }
}
